<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="mbName">
        <el-input
          v-model="queryParams.mbName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:ZrsMbManage:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:ZrsMbManage:edit']"
        >修改</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ZrsMbManageList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="模版名称" align="center" prop="mbName" />
      <el-table-column label="模版简称" align="center" prop="mbJname" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
      <el-table-column label="说明" align="center" prop="introduction" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="模版应用状态" align="center" prop="isEnable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_mb_is_enable" :value="scope.row.isEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="模版适用部门" align="center" prop="useDeptNames" />
      <el-table-column label="模版适用人员" align="center" prop="useUserNames" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.isEnable == '0'"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handlePlan(scope.row)"
            v-if="scope.row.isEnable == '0'"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >模版设计</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handlePlan(scope.row)"
            v-if="scope.row.isEnable == '1'"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >查看模版</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUpdateEnable(scope.row)"
            v-if="scope.row.isEnable == '0'"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >应用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-checked"
            @click="handleUpdateUnEnable(scope.row)"
            v-if="scope.row.isEnable == '1'"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >取消应用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleUpdateCopy(scope.row)"
            v-hasPermi="['business:ZrsMbManage:edit']"
          >复制模版</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.isEnable == '0'"
            v-hasPermi="['business:ZrsMbManage:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-loading"
            @click="handleCreateZrs(scope.row)"
            v-if="scope.row.isEnable == '1'"
            v-hasPermi="['business:ZrsMbManage:remove']"
          >责任书生成</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改责任书模版管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="mbName">
          <el-input v-model="form.mbName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="简称" prop="mbJname">
          <el-input v-model="form.mbJname" placeholder="请输入简称" />
        </el-form-item>
        <el-form-item label="模版适用" prop="mbType">
          <el-radio-group v-model="form.mbType">
            <el-radio
              v-for="dict in dict.type.zrs_use_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" rows="5"/>
        </el-form-item>
        <el-form-item label="说明" prop="introduction">
          <el-input v-model="form.introduction" type="textarea" placeholder="请输入说明"rows="5"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 生成责任书-->
    <el-dialog :title="title" :visible.sync="openCreateZrs" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" >{{form.mbName}}
        </el-form-item>
        <el-form-item label="生成部门" v-if="form.mbType == '1'">
          {{form.useDeptNames}}
        </el-form-item>
        <el-form-item label="生成人员" v-if="form.mbType == '2'">
          {{form.useUserNames}}
        </el-form-item>
        <el-form-item label="生效日期" prop="sxDate">
          <el-date-picker
            v-model="form.sxDate"
            type="date"
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注意" >
            <span style="color: red;">责任书生效日期每个月之生成一条信息，如果已经生成则不会重复生成责任书；</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCreateZrsForm">确 定</el-button>
        <el-button @click="cancelCreateZrs">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listZrsMbManage, getZrsMbManage, delZrsMbManage,
  addZrsMbManage, updateZrsMbManage,
  editEnable,editUnEnable,copyEdit ,generateZrsManage
        } from "@/api/business/ZrsMbManage";

export default {
  name: "ZrsMbManage",
  dicts: ['sys_status','sys_mb_is_enable','zrs_use_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 责任书模版管理表格数据
      ZrsMbManageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCreateZrs:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mbName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mbName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        mbJname: [
          { required: true, message: "简称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询责任书模版管理列表 */
    getList() {
      this.loading = true;
      listZrsMbManage(this.queryParams).then(response => {
        this.ZrsMbManageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelCreateZrs(){
      this.openCreateZrs = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mbId: null,
        mbName: null,
        mbJname: null,
        remark: null,
        introduction: null,
        status: '0',
        sxDate:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mbId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加责任书模版管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mbId = row.mbId || this.ids
      getZrsMbManage(mbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改责任书模版管理";
      });
    },
    handleUpdateCopy(row){
      const mbIds = row.mbId || this.ids;
      this.$modal.confirm('是否确定要复制责任书模版名称为【' + row.mbName + '】的数据项？').then(function() {
        return copyEdit(mbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("应用成功");
      }).catch(() => {});
    },
    handleUpdateEnable(row){
        const mbIds = row.mbId || this.ids;
        this.$modal.confirm('是否确定要应用责任书模版名称为【' + row.mbName + '】的数据项？').then(function() {
          return editEnable(mbIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("应用成功");
        }).catch(() => {});
    },
    handleUpdateUnEnable(row){
        const mbIds = row.mbId || this.ids;
        this.$modal.confirm('是否确定要取消应用责任书模版名称为【' + row.mbName + '】的数据项？').then(function() {
          return editUnEnable(mbIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("应用成功");
        }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mbId != null) {
            updateZrsMbManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZrsMbManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mbIds = row.mbId || this.ids;
      this.$modal.confirm('是否确认删除责任书模版管理名称为【' + row.mbName + '】的数据项？').then(function() {
        return delZrsMbManage(mbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 修改按钮操作 */
    handlePlan(row) {
      const mbId = row.mbId;
      const mbJname = row.mbJname;
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("[" + mbJname + "]模版设计", '/zhib/plan-edit/index/' + mbId, params);
    },
    //生成责任数据
    handleCreateZrs(row){
      this.reset();
      getZrsMbManage(row.mbId).then(response => {
        this.form = response.data;
        this.openCreateZrs = true;
        this.title = "选择日期生成责任书";
      });
    },
    submitCreateZrsForm(){
      if (this.form.sxDate == null) {
        this.$alert("生效日期不能为空");
        return;
      }
      generateZrsManage(this.form).then(response => {
        this.$modal.msgSuccess("生成完成！");
        this.openCreateZrs = false;
        this.getList();
      });
    }
  }
};
</script>
